var nowPage = 0;
var projectManagerId = 0;
var projectManagerType = 0;
var projectManager = {
	getData : function(id, type) {
		projectManagerId = id;
		projectManagerType = type;
		var name = $("#projectName").val();
		var startTime = $("#startTime").val();
		var endTime = $("#endTime").val();
		var mgrDepart = $("#mgrDepart").val();
		var plan = $("[name='plan']:checked").val();
		var ispass = $("[name='ispass']:checked").val();
		var comment = $("[name='comment']:checked").val();
		var checkout = $("[name='checkout']:checked").val();
		_projectManagerGetData = $("#pageShow").myPagination({
			currPage : 1,
			pageCount : 10,
			pageNumber : 5,
			panel : {
				tipInfo_on : true,
				links : "javascript:void()"
			},
			ajax : {
				onClick : function(page) {
					ZENG.msgbox.show(" 正在加载" + page + "页，请稍后...", 6, 600);
					nowPage = page;
				},
				on : true,
				type : "POST",
				dataType : 'json',
				async : false,
				url : "/app/project!getProjectsByCategory",
				param : {
					"c_id" : id,
					"type" : type,
					"projectName" : name,
					"startTime" : startTime,
					"endTime" : endTime,
					"mgrDepart" : mgrDepart,
					"plan" : plan,
					"ispass" : ispass,
					"comment" : comment,
					"checkout" : checkout
				},
				callback : function(data) {
					backUrl.addCookie(data.state);
					if ('success' == data.state) {
						pageCountId = data.pageCount;// 获取总页数
						projectManager.createContent(data.msg);
					} else if (data.state == "error") {
						errorAlert(data.msg);
					}
				}
			}
		});
	},
	createContent : function(data) {
		var pm = $("#projectManger");
		var content = "";
		pm.empty();
		if (data != null && data.length != 0) {
			$("#pageShow").show();
			content = "<tr id='titleTr'><th width='5%' scope='col'>"
					+ "<a href='javascript:void()' id='allCheckbox' onclick='checkBoxFunction()'>全选</a></th>"
					+ "<th width='22%' scope='col'>项目名称</th><th width='10%' scope='col'>实施时间</th>"
					+ "<th width='10%' scope='col'>总投资（亿）</th><th width='13%' scope='col'>主管部门</th>"
					+ "<th width='20%' scope='col'>资金来源</th><th width='20%' scope='col'>操作</th></tr>";
			for ( var int = 0; int < data.length; int++) {
				var v = data[int];
				var id = v.id;
				var name = v.name;
				var fund = v.fund;
				var impl = v.impl_time;
				var fundPlan = v.fund_plan;
				var mgrDepart = v.mgr_depart;
				var checkout = v.checkout;
				var comment = v.comment;

				var _fundPlan = fundPlan;
				if (fundPlan.length > 14) {
					fundPlan = fundPlan.substring(0, 13) + "...";
				}

				if (Number(checkout) == 1) {
					content += "<tr class='checkout'><td align='center'>";
				} else {
					content += "<tr><td align='center'><input name='checkboxProject' type='checkbox' value='" + id
							+ "'/>";
				}

				content += "</td><td align='center'>";

				if (Number(comment) == 2) {
					content += "<a href='javascript:viod()' style='float: left;' class='tempClass'>&nbsp;<font color='red'>*</font></a>";
				}

				if (null == fund || typeof (fund) == "undefined") {
					fund = "";
				}
				if (null == mgrDepart || typeof (mgrDepart) == "undefined") {
					mgrDepart = "";
				}
				if (null == fundPlan || typeof (fundPlan) == "undefined") {
					fundPlan = "";
				}

				content += "<a href='/app/project!getProjectDetailedById?p_id=" + id + "' target='_blank' title='"
						+ name + "' class='tempClass'>" + name + "</a></td><td align='center'>" + impl
						+ "</td><td align='center'>" + fund + "</td><td align='center' title='" + mgrDepart + "'>"
						+ mgrDepart + "</td><td align='center' title='" + _fundPlan + "'>" + fundPlan
						+ "</td><td align='center'>";

				if (Number(checkout) == 1) {
					content += "<a href='javascript:projectCheckout(" + id + ")'>校验</a>";
				}

				content += "<a href='javascript:projectUpdate.getData(" + id
						+ ")'>修改</a><a href='javascript:projectDelete.del(" + id + ")'>删除</a>";

				// if (Number(isPass) == 1 && Number(checkout) == 2) {
				// content += "<a href='javascript:projectPlan.getData(" + id +
				// ")'>列入计划</a>";
				// } else if (Number(isPass) == 2) {
				// content += "<a href='javascript:deletetPlanItem(" + id +
				// ")'>取消计划</a>";
				// }
				if (Number(checkout) == 2) {
					content += "<a href='javascript:planItem(" + id + ")'>计划管理</a>";
				}

				content += "</td></tr>";
			}
		} else {
			$("#pageShow").hide();
			content = "<tr id='noData'><td align='center'><h1>暂无数据</h1></td></tr>";
		}
		pm.append(content);
		projectManagerPoshytip.show();
	},
	jumpPage : function() {// 分页跳转
		_projectManagerGetData.jumpPage(nowPage);
	}
};
/**
 * 修改
 */
var projectUpdate = {
	getData : function(id) {
		window.open("/app/project!jumpProjectUpdate?p_id=" + id);
	}
};
/**
 * 删除
 */
var projectDelete = {
	del : function(id) {
		art.dialog.confirm("确认删除？", function() {
			$.ajax({
				dataType : "json",
				type : "post",
				data : {
					"p_id" : id
				},
				url : "/app/project!deleteProject",
				success : function(data) {
					backUrl.addCookie(data.state);
					if (data.state == "success") {
						tipsAlert(data.msg);
						projectManager.jumpPage();
					} else if (data.state == "error") {
						errorAlert(data.msg);
					}
				}
			});
		});
	}
};

var projectManagerPoshytip = {
	show : function() {
		$("#projectManager").find("td").poshytip({
			className : 'tip-darkgray',
			showOn : 'hover',
			alignTo : 'target',
			alignX : 'center',
			alignY : "center",
			allowTipHover : true,
			fade : true,
			slide : true
		});
		$(".tempClass").poshytip({
			className : 'tip-darkgray',
			showOn : 'hover',
			alignTo : 'target',
			alignX : 'right',
			alignY : "center",
			offsetX : 10,
			allowTipHover : true,
			fade : true,
			slide : true
		});
	}
};
/**
 * 点击左侧类别树后执行的该方法
 * 
 * @param id
 * @param type
 */
function categoryClickProjectSearch(id, type) {
	projectManagerReset();
	projectManager.getData(id, type);
}
/**
 * 重置
 */
function projectManagerReset() {
	$("#projectName").val("");
	$("#startTime").val("");
	$("#endTime").val("");
	$("#mgrDepart").val("");
	$("#category").removeAttr("checked");

	$("[name='ispass']").eq(0).prop('checked', true);
	$("[name='plan']").eq(0).prop('checked', true);
	$("[name='comment']").eq(0).prop('checked', true);
	$("[name='checkout']").eq(0).prop('checked', true);
}
/**
 * 全选、全不选
 */
var checkBoxFunctionBoolean = true;
function checkBoxFunction() {
	if (checkBoxFunctionBoolean == true) {
		$("input[name='checkboxProject']").prop("checked", true);
		$("#allCheckbox").text("取消");
		$("#allCheckbox2").text("取消");
	} else {
		$("input[name='checkboxProject']").prop('checked', false);
		$("#allCheckbox").text("全选");
		$("#allCheckbox2").text("全选");
	}
	checkBoxFunctionBoolean = !checkBoxFunctionBoolean;
}
/**
 * 反选
 */
function checkboxFunction2() {
	$("input[name='checkboxProject']").each(function() {
		this.checked = !this.checked;
	});
	checkBoxFunctionBoolean = true;
	$("#allCheckbox").text("全选");
	$("#allCheckbox2").text("全选");
}

function planItem(id) {
	art.dialog.open("/app/plan!jumpPlanItem?projectId=" + id, {
		id : "info",
		lock : true,
		drag : false,
		title : '',
		width : "500px",
		height : "600px"
	});
}
/**
 * 批量列入计划
 */
var BatchJionPlan = {
	check : function() {
		var thisValues = "";
		$("input[name='checkboxProject']:checked").each(function() {
			thisValues += this.value + ",";
		});
		if (!checkValue(thisValues)) {
			errorAlert("请选择项目！");
			return false;
		} else {
			BatchJionPlan.getData(thisValues);
		}
	},
	getData : function(projectIds) {
		$.ajax({
			dataType : "json",
			type : "post",
			url : "/app/plan!getAllPlan",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state == "success") {
					BatchJionPlan.show(data.msg, projectIds);
				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	},
	show : function(data, projectIds) {
		art.dialog({
			id : "addPlanItem",
			lock : true,
			content : BatchJionPlan.creatContent(data),
			button : [ {
				name : '确定',
				callback : function() {
					var planId = $("[name='radioPlanId']:checked").val();
					if (!checkValue4(planId)) {
						alert("请选择计划！");
						return false;
					} else {
						BatchJionPlan.addPlanItem(projectIds, planId);
						this.close();
					}
				},
				focus : true
			} ],
			cancelVal : '取消',
			cancel : true
		});
	},
	addPlanItem : function(projectIds, planId) {
		$.ajax({
			dataType : "json",
			type : "post",
			data : "planId=" + planId + "&projectIds=" + projectIds,
			url : "/app/plan!addBulkPlanItem",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state == "success") {
					tipsAlert(data.msg);
					projectManager.jumpPage();
				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	},
	creatContent : function(data) {
		var content = "暂无数据，请先添加计划！";
		if (null != data && 0 < data.length) {
			content = "<div class='sz_8_tc'><table width='400px' border='0' cellspacing='1' cellpadding='3'>"
					+ "<tr><th width='46' scope='col' align='center'>选择</th>"
					+ "<th width='301' scope='col' align='center'>计划名称</th></table>"
					+ "<div class='sz_8_tc_nr'><table width='400px' border='0' cellspacing='1' cellpadding='3' >";
			for ( var int = 0; int < data.length; int++) {
				var v = data[int];
				var id = v.id;
				var name = v.name;
				content += "<tr><td width='23' align='center'><input name='radioPlanId' type='radio' value='" + id
						+ "' /></td><td width='338' align='center'>" + name + "</td></tr>";
			}
			content += "</table></div></div>";
		}
		return content;
	}
};
/**
 * 校验项目
 * 
 * @param id
 */
function projectCheckout(id) {
	art.dialog.confirm('确认该项目已经完成校验？', function() {
		$.ajax({
			dataType : "json",
			type : "post",
			data : "p_id=" + id,
			url : "/app/project!projectCheckout",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state == "success") {
					tipsAlert(data.msg);
					projectManager.jumpPage();
				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	});
}

var onloadProjectManager = {
	onload : function() {
		projectManager.getData();
		$("#startTime").focus(function() {
			WdatePicker({
				dateFmt : 'yyyy-MM'
			});
		});
		$("#endTime").focus(function() {
			WdatePicker({
				dateFmt : 'yyyy-MM'
			});
		});
		$("[name='plan']").click(function() {
			$("#search").click();
		});
		$("[name='checkout']").click(function() {
			$("#search").click();
		});
		$("[name='ispass']").click(function() {
			$("#search").click();
		});
		$("[name='comment']").click(function() {
			$("#search").click();
		});
		$("#category").change(function() {
			$("#search").click();
		});
		$("#search").click(function() {
			var tof = $("#category").is(":checked");
			if (tof == true) {
				projectManagerId = 0;
				projectManagerType = 0;
				$("#store").find("ul").slideUp();
				$("#store").find(".sz_nav_left_xz").removeClass("sz_nav_left_xz");
				$("#store").find(".jian").removeClass("jian").addClass("jia");
			}
			projectManager.getData(projectManagerId, projectManagerType);
		});
		$("#reset").click(function() {
			projectManagerReset();
		});
	}
};